package com.draven.utils;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Draven on 2017/4/13.
 * E-mail:draven1122@163.com
 */
public class JsonDateValueProcessor implements JsonValueProcessor {

    private String datePattern = "yyyy-MM-dd HH-mm:ss";

    public JsonDateValueProcessor(String dateFormatter){
        this.datePattern = dateFormatter;
    }
    public Object processArrayValue(Object value, JsonConfig config) {
        return process(value);
    }

    public Object processObjectValue(String key, Object value, JsonConfig config) {
        return process(value);
    }

    private Object process(Object value){

        if(value instanceof Date){
            SimpleDateFormat sdf = new SimpleDateFormat(this.datePattern);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }
}